package hot150;

// https://leetcode.cn/problems/rotate-array/description/?envType=study-plan-v2&envId=top-interview-150
public class q6 {

    public void rotate(int[] nums, int k) {
        int n = nums.length;
        k %= n;
        method1(nums, k, n);
    }

    /**
     * 翻转数组
     * 0ms
     * 复杂度O(n)
     */
    public void method1(int[] nums , int k, int n) {
        // 全部翻转
        reverse(nums, 0, n - 1);
        // 翻转前k个
        reverse(nums, 0, k - 1);
        // 翻转后n-k个
        reverse(nums, k, n - 1);
    }

    public void reverse(int[]nums, int start, int end) {
        while (start < end) {
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end--;
        }
    }
}
